<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="${objectName}Mapper" >

  <resultMap id="BaseResultMap" type="${package}.model.${objectName}" >
  	<#list fieldList as var>
	<result column="${var.columnName}" property="${var.classAttribute}" />
	</#list>
  </resultMap>
  
  <sql id="Base_Column_List" >
    <#list fieldList as var>
    <#if var_has_next>
	${var.columnName},
    <#else>
	${var.columnName}
    </#if>
	</#list>
  </sql>
  
  <sql id="queryCondition">
	<where>
	<#list fieldList as var>
	  <if test="${var.classAttribute} != null">
		and ${var.columnName} =  ${r"#{"}${var.classAttribute}${r"}"}		
	  </if>
	</#list>
  	</where>
  </sql>
  
  <!-- 总数查询 -->
  <select id="count" parameterType="${package}.model.${objectName}" resultType="java.lang.Integer">
	select count(1) from ${tableName}
	<include refid="queryCondition"/>
  </select>
  
  <!-- 分页查询 -->
  <select id="selectPagination" resultMap="BaseResultMap" parameterType="java.util.Map" >
    select 
    <include refid="Base_Column_List" />
    from ${tableName}
    <include refid="queryCondition"/>
    <if test="index != null and size != null">
		limit ${r"#{index}"},${r"#{size}"}
	</if>
  </select>

  <!-- 条件查询 -->
  <select id="selectOne" resultMap="BaseResultMap" parameterType="${package}.model.${objectName}" >
    select 
    <include refid="Base_Column_List" />
    from ${tableName}
    <include refid="queryCondition"/>
    limit 1
  </select>

  <!-- 条件查询 -->
  <select id="selectList" resultMap="BaseResultMap" parameterType="${package}.model.${objectName}" >
    select 
    <include refid="Base_Column_List" />
    from ${tableName}
    <include refid="queryCondition"/>
  </select>
  
  <!-- 根据条件删除-->
  <!-- 禁止物理删除数据
  <delete id="deleteByCondition" parameterType="${package}.model.${objectName}" >
    delete from ${tableName}
    <include refid="queryCondition"/>
  </delete>
  -->
  
  
  <!-- 全量插入 -->
  <insert id="insert" parameterType="${package}.model.${objectName}" useGeneratedKeys="true" keyProperty="id" >
    insert into ${tableName} (<include refid="Base_Column_List" />)
    values (
	<#list fieldList as var>
			<#if var_has_next && var.classAttribute = "createTime">
				sysdate(),
			<#elseif var.classAttribute = "updateTime">
				sysdate()
			<#elseif var_has_next>
				${r"#{"}${var.classAttribute}${r"}"},
	    		</#if>
	    
	</#list>
	)
  </insert>
  
  <!-- 选择性插入 -->
  <insert id="insertSelective" parameterType="${package}.model.${objectName}" useGeneratedKeys="true" keyProperty="id" >
    insert into ${tableName}
    <trim prefix="(" suffix=")" suffixOverrides="," >
    <#list fieldList as var>
    		<#if var.classAttribute = "createTime" || var.classAttribute = "updateTime">
		    	${var.columnName} ,	
    		<#else>
		  <if test="${var.classAttribute} != null">
			${var.columnName} ,	
		  </if>
    		</#if>
	</#list>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
    <#list fieldList as var>
    		<#if var.classAttribute == "createTime" || var.classAttribute == "updateTime">
		    sysdate() ,	
    		<#else>
		  	<if test="${var.classAttribute} != null">
				${r"#{"}${var.classAttribute}${r"}"} ,	
	  		</if>
    		</#if>
	</#list>
    </trim>
  </insert>
  
  <!-- 选择性更新 -->
  <update id="updateSelective" parameterType="${package}.model.${objectName}" >
    update ${tableName}
    <set >
      <#list fieldList as var>
      	<#if var.classAttribute != "createTime" && var.classAttribute != "updateTime">
      		<if test="${var.classAttribute} != null " >
	        		${var.columnName} = ${r"#{"}${var.classAttribute}${r"}"},
	      	</if>
      	</#if>
	  </#list>
    </set>
    where id=${r"#{id}"}	
  </update>
  
  
</mapper>